﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.DataConvertLib;

namespace 随机模拟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ReadExcel();
        }
        private List<Variable> variables;
        private Dictionary<string, string> dic = new Dictionary<string, string>();
        private void ReadExcel()
        {
            try
            {
                dic.Add("VariableName", "变量名称");
                dic.Add("VariableAddress", "变量地址");
                dic.Add("VariableType", "变量类型");
                variables = ExcelHelper.ExcelToList<Variable>(Application.StartupPath + "\\地址表.xlsx", dic, 0, 0);
                if (variables.Count > 0) 
                {
                    //读取出来
                    for (int i = 0; i < variables.Count; i++)
                    {
                        DicVariable.Add(variables[i].VariableName, variables[i]);
                    }
                }
                else
                {
                    MessageBox.Show("加载Excel地址文件失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        bool isConnect = false;
        NModbusTcp nModbusTcp = new NModbusTcp();
        public Dictionary<string, Variable> DicVariable { get; set; } = new Dictionary<string, Variable>();
        private void button1_Click(object sender, EventArgs e)
        {
            if (isConnect == false && btnConnectOrClose.Text == "连接")
            {
                var res = nModbusTcp.Connect("127.0.0.1", 8888);
                if (res == 2)
                {
                    isConnect = false;
                    btnConnectOrClose.BackColor = Color.Red;
                }
                else
                {
                    isConnect = true;
                    btnConnectOrClose.BackColor = Color.Green;
                    btnConnectOrClose.Text = "断开";
                }
            }
            else
            {
                nModbusTcp.Disconnect();
                isConnect = false;
                btnConnectOrClose.Text = "连接";
                btnConnectOrClose.BackColor = Color.White;
            }
        }
        bool value1 = false;

        private CancellationTokenSource cts ;
        Random r = new Random();
        bool isRun = false;
        ushort s1 = 0;
        private void button1_Click_1(object sender, EventArgs e)
        {
            cts = new CancellationTokenSource();
            if (isConnect && btnStartOrStop.Text == "开始")
            {
                isRun = true;
                Task.Run(() =>
                {
                    while (!cts.IsCancellationRequested)
                    {
                        if (isRun)
                        {                                                     
                            foreach (var item in DicVariable.Values)
                            {
                                if (item.VariableType == "bool")
                                {
                                    var num = r.Next(1, 10);
                                    if (num % 2 == 0)
                                    {
                                        value1 = true;
                                    }
                                    else
                                    {
                                        value1 = false;

                                    }
                                    nModbusTcp.WriteSingleCoil(item.VariableAddress, value1);
                                }
                                else if (item.VariableType == "float")
                                {
                                    if (item.VariableName.Contains("PH"))
                                    {
                                        var number = r.Next(10, 140);
                                        var numberF = number / 10.0f;
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat(numberF);
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs,0,DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if(item.VariableName.Contains("输送机"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(10, 1000) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if(item.VariableName.Contains("厂房温度"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 400) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if (item.VariableName.Contains("湿度"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(2, 6) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if(item.VariableName.Contains("固化炉温度"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 2000) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if (item.VariableName.Contains("水分炉温度"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 2000) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if (item.VariableName.Contains("脱脂装置液位"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 10) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                    else if (item.VariableName.Contains("粗洗装置液位"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 10) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    } 
                                    else if (item.VariableName.Contains("精洗装置液位"))
                                    {
                                        byte[] bs = ByteArrayLib.GetByteArrayFromFloat((r.Next(0, 10) / 10.0f));
                                        ushort usArray = UShortLib.GetUShortFromByteArray(bs, 0, DataFormat.ABCD);
                                        nModbusTcp.WriteSingleRegister(item.VariableAddress, usArray);
                                    }
                                }
                                else if (item.VariableType == "short")
                                {
                                    nModbusTcp.WriteSingleCoil(item.VariableAddress, value1);
                                }
                            }
                        }
                        Task.Delay(2000);
                    }
                });
                btnStartOrStop.Text = "停止";
                btnStartOrStop.BackColor = Color.Green;
            }
            else
            {
                isRun = false;
                cts.Cancel();
                btnStartOrStop.Text = "开始";
                btnStartOrStop.BackColor = Color.White;
            }
        }
    }
}
